<?php

namespace app\business\controller;

use app\business\model\CustomerNatureListModel;
use cmf\controller\AdminBaseController;

/**
 * Class LicensePlateController
 * @package app\business\controller
 * @adminMenuRoot(
 *     'name'   =>'属性配置',
 *     'action' =>'default',
 *     'parent' =>'business/LicensePlate/default',
 *     'display'=> true,
 *     'order'  => 30,
 *     'icon'   =>'th',
 *     'remark' =>'属性配置',
 * )
 */
class CustomerNatureListController extends AdminBaseController
{
    /**
     * 客户属性列表
     * @adminMenu(
     *     'name'   => '客户属性列表',
     *     'parent' => 'default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '客户属性列表',
     *     'param'  => ''
     * )
     */
    function index()
    {
        $customerNatureListModel = new CustomerNatureListModel();
        $lists = $customerNatureListModel->paginate(10);

        $page = $lists->render();
        $this->assign('lists', $lists);
        $this->assign('page', $page);

        return $this->fetch();
    }

    /**
     * 添加
     * @adminMenu(
     *     'name'   => '添加',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加',
     *     'param'  => ''
     * )
     */
    function add()
    {
        if ($this->request->isPost()) {
            $customerNatureListModel = new CustomerNatureListModel();
            $arrData = $this->request->post();

            $result = $customerNatureListModel->allowField(true)->validate(true)->save($arrData);
            if ($result) {
                $this->success(lang("ADD_SUCCESS"), url("index"));
            } else {
                $this->error($customerNatureListModel->getError());
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑
     * @adminMenu(
     *     'name'   => '编辑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑',
     *     'param'  => ''
     * )
     */
    function edit()
    {
        $customerNatureListModel = new CustomerNatureListModel();
        if ($this->request->isPost()) {
            $arrData = $this->request->post();

            $result = $customerNatureListModel->allowField(true)->validate(true)->save($arrData, ["id" => $arrData["id"]]);
            if ($result !== false) {
                $this->success(lang("EDIT_SUCCESS"), url("index"));
            } else {
                $this->error($customerNatureListModel->getError());
            }
        }
        $intId = $this->request->param("id", 0, 'intval');

        $result = $customerNatureListModel->where(["id" => $intId])->find();

        $this->assign('result', $result);
        return $this->fetch();
    }
}